package com.tencent.wxcloudrun.utils;


import com.tencent.wxcloudrun.model.Visit;
import org.springframework.scheduling.annotation.Scheduled;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DeviceUtils {
    public static Map<String, Visit> cache = new HashMap<String,Visit>();


    // 定时清楚cache
    @Scheduled(fixedDelay = 120 * 1000)
    public void timer(){
        cache.clear();
    }

    // 每个设备的每次访问上限次数
    public static final int VISIT_LIMIT = 2;
    public static final int IOS_VISIT_LIMIT = 1;

    public static int getVisitLimit(String deviceType){
        if("iOS".equalsIgnoreCase(deviceType)){
            return IOS_VISIT_LIMIT;
        }else if("Android".equalsIgnoreCase(deviceType)){
            return VISIT_LIMIT;
        }else {
            return IOS_VISIT_LIMIT;
        }
    }

    /**
     * 生成唯一的会话标识
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    // 根据设备id获取用户访问次数
    public static int getVisitNum(String deviceId) {
        Visit element = cache.get(deviceId);
        if(element!=null){
            return element.getVisitNum();
        }
        return 0;
    }

    // 根据设备id获取用户访问次数
    public static String getVisitAddress(String deviceId) {
        Visit element = cache.get(deviceId);
        if(element!=null){
            return element.getAddress();
        }
        return "";
    }
    // 存储用户访问记录
    public static void saveDeviceCache(String deviceId,int visitNum,String address) {
        Visit element = new Visit(visitNum,address);
        cache.put(deviceId,element);
    }
    // 删除用户访问次数
    public static void deleteDeviceCache(String deviceId) {
        cache.remove(deviceId);
    }

    public static String checkDevice(HttpServletRequest request){
        // 获取User-Agent头部
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null) {
            return "无法获取设备信息";
        }
        // 解析设备类型
        String deviceType;
        if (userAgent.contains("Android")) {
            deviceType = "Android";
        } else if (userAgent.contains("iPhone") || userAgent.contains("iPad") || userAgent.contains("iOS")) {
            deviceType = "iOS";
        } else {
            deviceType = "other";
        }
        return deviceType;
    }

    // 工具方法：获取真实IP（处理代理场景，如微信服务器转发）
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 多代理场景下，取第一个IP
        if (ip.contains(",")) {
            ip = ip.split(",")[0].trim();
        }
        return ip;
    }
}
